package object_oriented;

public class Decimal {
    // 声明一个用来保存整数的字段
    private final long value ; // 注意这里封装的是 long 类型的数值

    // 声明一个可以接受单个整数值的构造方法
    public Decimal( long value ) {
        this.value = value ;
    }

    public String toBinaryString () {  //十进制转二进制
        long temp ;
        String str = new String() ;
        temp = this.value;
        while( temp / 2 != 0 ){

            str = str + ( temp % 2 ) ;
            temp = temp / 2 ;
        }
        if ( this.value % 2 != 0) {
            str = str + "1" ;
        }
        StringBuffer str2 =new StringBuffer( str ) ;
        return str2.reverse().toString() ;
    }

    public String toHexString () {   //十进制转十六进制
        long temp ;
        String str = new String() ;
        temp = this.value;
        while( temp / 16 != 0 ){

            str = str + ( temp % 16 ) ;
            temp = temp / 16 ;
        }
        if ( this.value % 16 != 0) {
            if ( (this.value % 16) < 10){

                str = str + (this.value % 16) ;
            }else {
                switch ((int)this.value % 16){
                    case 10:str = str + "A" ;break;
                    case 11:str = str + "B" ;break;
                    case 12:str = str + "C" ;break;
                    case 13:str = str + "D" ;break;
                    case 14:str = str + "E" ;break;
                    case 15:str = str + "F" ;break;
                }
            }

        }
        StringBuffer str2 =new StringBuffer( str ) ;
        return str2.toString() ;
    }
}
